<?php
namespace app\index\controller;

use think\Request;
use think\Db;

use app\index\model\getCash as GetCashModel;

class GetCash
{
    //获取边缘id
    public function getBorderId($keyName, $keyWord, $page, $dataNum)
    {
        $getCashTab = 'dcw_fastask_getCashDetail';
        $limit = ($page - 1) * $dataNum;
        $sql = '';
        $borderId = 0;
        if ($keyName == '') {
            if ($limit == 0) {
                $sql = "SELECT id FROM $getCashTab
                        ORDER BY id DESC
                        LIMIT 1
                ";
                $res = Db::query($sql);
                $borderId = $res == NULL ? 0 : $res[0]['id'] + 1;
            }
            else {
                $sql = "SELECT id FROM $getCashTab
                    ORDER BY id DESC
                    LIMIT $limit
                ";
                $allId = Db::query($sql);
                $borderId = $allId == NULL ? 0 : $allId[count($allId) - 1]['id'];        
            }
        }
        else {
            if ($limit == 0) {
                $sql = "SELECT id FROM $getCashTab
                        WHERE $keyName = '$keyWord'
                        ORDER BY id DESC
                        LIMIT 1
                ";
                $res = Db::query($sql);
                $borderId = $res == NULL ? 0 : $res[0]['id'] + 1;
            }   
            else {
                $sql = "SELECT id FROM $getCashTab
                    WHERE $keyName = '$keyWord'
                    ORDER BY id DESC
                    LIMIT $limit
                ";
                $allId = Db::query($sql);
                $borderId = $allId == NULL ? 0 : $allId[count($allId) - 1]['id']; 
            }
        }
        
        return $borderId;
    }
    public function getCashNote(Request $request)
    {
        //获取请求参数
        $page = $request->param('page');
        $dataNum = $request->param('dataNum');
        $keyName = $request->param('keyName');
        $keyWord = $request->param('keyWord');

        //初始化消息
        $message = '';
        $status = 0;


        //获取边缘id
        $borderId = $this->getBorderId($keyName, $keyWord, $page, $dataNum);
        
        //构造sql
        $totalSql = '';
        $getCashTab = 'dcw_fastask_getcashdetail';
        $userTab = "dcw_fastask_user";
        if ($keyName == '') {
            $totalSql = "SELECT COUNT(*) AS dataTotal FROM $getCashTab";
            $sql = "SELECT * FROM $getCashTab
                    WHERE id < $borderId
                    ORDER BY id DESC
                    LIMIT $dataNum
            ";
            
        }
        else {
            $totalSql = "SELECT COUNT(*) AS dataTotal FROM $getCashTab WHERE $keyName='$keyWord'";
            $sql = "SELECT * FROM $getCashTab
                    WHERE id < $borderId
                    AND $keyName = '$keyWord'
                    ORDER BY id DESC
                    LIMIT $dataNum
            ";
        }

        $allNote = Db::query($sql);

        //获取数据总数目
        $note = Db::query($totalSql);
        $dataTotal = $note == NULL ? 0 : $note[0]['dataTotal'];

        //返回消息
        $data = [
            "allNote" => $allNote,
            "dataTotal" => $dataTotal
        ];
        $message = 'get cash note success.';
        $JsonData = controller('index/JsonData');
        $json = $JsonData->getJsonData($status, $message, $data);
        return $json;

        
    }
}